<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Spbu_model extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function get_all() {
		return $this->db->get('TblSPBU')->result_array();
	}

	function get_one($id) {
		$query = $this->db->get_where('TblSPBU', array('id' => $id));
		if ($query->num_rows() > 0) {
			return $query->row_array();
		} else {
			return FALSE;
		}
	}
        
        function get_one_by_nospj($nospj) {
                $this->db->join('TblSPBU','TblSPBU.id=TblSPBUVoucher.idspbu','inner');
		$query = $this->db->get_where('TblSPBUVoucher', array('NoSPJ' => $nospj));
		if ($query->num_rows() > 0) {
			return $query->row_array();
		} else {
			return FALSE;
		}
	}

	function insert_new($data) {
		$this->db->insert('TblSPBU', $data);
	}

	function update_old($id, $data) {
		$this->db->where('id', $id);
		$this->db->update('TblSPBU', $data);
	}

	function delete_old($id) {
		$this->db->delete('TblSPBU', array('id' => $id));
	}

	function get_spbu_voucher_join_setoran($tgl_awal, $tgl_akhir) {
		if ($tgl_awal == '') {
			$tgl_awal = date('Ymd');
		} else {
			$tgl_awal = $this->date_func->change_format_date($tgl_awal, 'd-m-Y', 'Ymd');
		}
		if ($tgl_akhir == '') {
			$tgl_akhir = date('Ymd');
		} else {
			$tgl_akhir = $this->date_func->change_format_date($tgl_akhir, 'd-m-Y', 'Ymd');
		}
		$this->db->select("*,TblSPBU.nama AS namaspbu");
		$this->db->from('TblSPBU');
		$this->db->join('TblSPBUVoucher', 'TblSPBUVoucher.idspbu = TblSPBU.kode_spbu', 'inner');
		$this->db->join('TblSetoran', 'TblSPBUVoucher.NoSPJ = TblSetoran.NoSPJ', 'inner');
		$this->db->join('TbUser', 'TblSetoran.idkasirSetoran = TbUser.user_id', 'inner');
		$this->db->where('tanggal BETWEEN "' . $tgl_awal . '" AND "' . $tgl_akhir . '"');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_spbu_voucher_join_setoran_cabang($tgl_awal, $tgl_akhir,$cabang) {
		if ($tgl_awal == '') {
			$tgl_awal = date('Ymd');
		} else {
			$tgl_awal = $this->date_func->change_format_date($tgl_awal, 'd-m-Y', 'Ymd');
		}
		if ($tgl_akhir == '') {
			$tgl_akhir = date('Ymd');
		} else {
			$tgl_akhir = $this->date_func->change_format_date($tgl_akhir, 'd-m-Y', 'Ymd');
		}
		$this->db->select("*,TblSPBU.nama AS namaspbu");
		$this->db->from('TblSPBU');
		$this->db->join('TblSPBUVoucher', 'TblSPBUVoucher.idspbu = TblSPBU.kode_spbu', 'inner');
		$this->db->join('TblSetoran', 'TblSPBUVoucher.NoSPJ = TblSetoran.NoSPJ', 'inner');
		$this->db->join('TbUser', 'TblSetoran.idkasirSetoran = TbUser.user_id', 'inner');
		$this->db->where('tanggal BETWEEN "' . $tgl_awal . '" AND "' . $tgl_akhir . '"');
		$this->db->where('TblSetoran.cabang_keberangkatan',$cabang);
		$query = $this->db->get();
		return $query->result_array();
	}

}

/** End of file */